package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.CalendarEventTypeDAO;
import org.jetnuke.entities.CalendarEventType;

public class GenericCalendarEventTypeDAO extends AutoKeys implements
		CalendarEventTypeDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericCalendarEventTypeDAO.class);

	public CalendarEventType selectByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("CalendarEventTypeModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		CalendarEventType cet = new CalendarEventType();

		if (rs.next()) {
			fillCalendarEventTypeFromResultSet(cet, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPK(int) - end"); //$NON-NLS-1$
		}
		return cet;
	}

	protected void fillCalendarEventTypeFromResultSet(CalendarEventType cet,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillCalendarEventTypeFromResultSet(CalendarEventType, ResultSet) - start"); //$NON-NLS-1$
		}

		cet.setID(rs.getInt("ID"));
		cet.setDESC(rs.getString("EVTYPE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillCalendarEventTypeFromResultSet(CalendarEventType, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("CalendarEventTypeModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			CalendarEventType u = new CalendarEventType();
			fillCalendarEventTypeFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
